// Written in the D Programming Language
/*
 * Copyright (c) 2008 OpenGLUtils
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'OpenGLUtils' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
module openglutils.GLShader;

import openglutils.GL;
import openglutils.GLObject;
import openglutils.GLVersion;

import tango.stdc.stringz;

private class GLShader(GLuint T): GLObject
{
	/**
	 * Constructs a new GLShader.
	 * 
	 * Throws: GLException if the OpenGL driver doesn't support version 2.0 or higher
	 */
	public this()
	{
		super();

		if(getGLVersion() < GLVersion.Version20)
		{
			throw new GLException("OpenGL version 2.0 is required.");
		}
	}

	/**
	 * Sets the sources for this shader.
	 * 
	 * Params:
	 * 	sources = an array of strings containing all the sources for this shader
	 */
	public void setSources(char[][] sources)
	{
		glShaderSource(getId(), sources.length, convertSourceStrings(sources), null);
	}

	/**
	 * Compiles this shader.
	 */
	public void compile()
	{
		glCompileShader(getId());
	}

	/**
	 * A convenience function for calling setSources and compile.
	 * 
	 * Params:
	 *     sources = an array of strings containing all the sources for this shader
	 */
	public void compileSources(char[][] sources)
	{
		setSources(sources);
		compile();
	}
	
	/**
	 * Gets the value for the given parameter.
	 * 
	 * Params:
	 * 	pname = the program parameter name
	 * 
	 * Returns: The parameter's value.
	 */
	public GLint getParameter(GLenum pname)
	{
		GLint param;
		glGetShaderiv(getId(), pname, &param);
		return param;
	}

	/**
	 * Gets the length of the current info log for this program.
	 * 
	 * Returns: The info log length
	 */
	public GLint getInfoLogLength()
	{
		return getParameter(GL_INFO_LOG_LENGTH);
	}

	/**
	 * Gets the info log for this shader.
	 * 
	 * Returns: The info log if there is something, an empty string otherwise.
	 */
	public char[] getInfoLog()
	{
		GLint length;
		char[] log = new char[getInfoLogLength()];
		glGetShaderInfoLog(getId(), getInfoLogLength(), &length, log.ptr);
		return length > 0 ? log : "";
	}

	/**
	 * Generates the id for this OpenGL shader.
	 * 
	 * Returns: The generated id.
	 */
	protected override GLuint generateID()
	{
		return glCreateShader(T);
	}

	/**
	 * Destroys this shader.
	 */
	public override void destroy()
	{
		return glDeleteShader(getId());
	}

	/**
	 * Checks if the given parameter is a valid shader.
	 * 
	 * Returns: True if the parameter is a shader, false otherwise.
	 */
	public static bool isShader(GLuint shader)
	{
		return glIsShader(shader) == GL_TRUE;
	}

	private char** convertSourceStrings(char[][] sources)
	{
		char*[] dst;
		dst.length = sources.length;
		
		foreach(i, s; sources)
		{
			dst[i] = toStringz(s);
		}
		return dst.ptr;
	}
}

/**
 * GLVertexShader is an implementation for OpenGL vertex shader objects. It provides an interface for functions 
 * pertaining to vertex shaders in OpenGL.
 */
public alias GLShader!(GL_VERTEX_SHADER) GLVertexShader;

/**
 * GLFragmentShader is an implementation for OpenGL fragment shader objects. It provides an interface for functions 
 * pertaining to fragment shaders in OpenGL.
 */
public alias GLShader!(GL_FRAGMENT_SHADER) GLFragmentShader;
